
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import org.json.me.JSONArray;
import org.json.me.JSONObject;

public class f_readplaylist extends PlayListView implements CommandListener {

    private Command CMD_BACK;
    private Command CMD_SEARCH;
    private Command CMD_PLAYER;
    private Command CMD_DELETE;
    private Command CMD_EDIT;
    private int type;
    private int curView;

    public f_readplaylist(int type) {
        CMD_BACK = new Command("Back", Command.BACK, 0);
        CMD_SEARCH = new Command("Search", Command.SCREEN, 0);
        CMD_PLAYER = new Command("Now playing", Command.SCREEN, 0);
        CMD_DELETE = new Command("Delete", Command.SCREEN, 0);
        CMD_EDIT = new Command("Detail", Command.SCREEN, 0);
        if (type == 0) {
            addCommand(CMD_BACK);
            addCommand(CMD_SEARCH);
            addCommand(CMD_PLAYER);
        } else if (type == 1) {
            addCommand(CMD_BACK);
            addCommand(CMD_PLAYER);
            addCommand(CMD_EDIT);
        } else if (type == 2) {
            this.addCommand(CMD_BACK);
        }
        setCommandListener(this);
    }

    public void load(String url) {
        Playlist[] tmp_playlist;
        try {
            HttpConnection hc = (HttpConnection) Connector.open(AppManager.SERVER + url);
            InputStream is = hc.openInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            char[] data = new char[1];
            String str_json = "";
            while (isr.read(data) != -1) {
                str_json += new String(data);
            }
            hc.close();
            JSONArray ja = new JSONArray(str_json);
            tmp_playlist = new Playlist[ja.length() - 1];
            JSONObject jo;
            for (int i = 0; i < ja.length() - 1; i++) {
                jo = ja.getJSONObject(i);
                tmp_playlist[i] = new Playlist(jo.getString("spl_id"), jo.getString("spl_name"), jo.getString("casi_name"), 0);
            }
            this.loadItems(tmp_playlist);
        } catch (Exception ex) {
        }
    }

    public void onSelect(int selectedIndex) {
        try {
            HttpConnection hc = (HttpConnection) Connector.open(AppManager.SERVER + "count.php?type=playlist&id=" + playlist[selectedIndex].code);
            hc.getResponseCode();
            hc.close();
        } catch (Exception ex) {
        }
        AppManager.F_PLAYER.load(this.playlist[selectedIndex]);
        AppManager.open(AppManager.F_PLAYER);
        AppManager.F_PLAYER.playStart();

    }

    public void commandAction(Command c, Displayable d) {
        if (c == CMD_BACK) {
            AppManager.back();
        }
        if (c == CMD_SEARCH) {
            AppManager.open(new Search());
        }
    }

    public void onLeftRight() {
        AppManager.swap();
    }
}
